package com.lw.controller;

import com.lw.common.IJsonResult;
import com.lw.pojo.VideoInfo;
import com.lw.service.VideoServiceImpl;
import com.lw.utils.PagedResult;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

@RestController
public class VideoController {

    @Value("${file.base.path:/temp/user/videos/}")
    private String fileBasePath = "/temp/files";//文件上传路径

    @Autowired
    private VideoServiceImpl videoService;

    @PostMapping ("/queryAllVideos")
    @ApiOperation(value="上传视频",notes = "上传视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value = "当前页",required = true
                    ,dataType = "Integer",paramType = "query") ,
            @ApiImplicitParam(name="pageSize",value = "每页条数",required = true
                    ,dataType = "Integer",paramType = "query")
    })
    public IJsonResult queryAllVideos(Integer page, Integer pageSize){
        VideoInfo param = new VideoInfo();
//        param.setVideoName("奔月");
        PagedResult result = videoService.queryAllVideos(page,pageSize,param);
        return IJsonResult.ok(result);
    }


    @ApiOperation(value="上传视频",notes = "上传视频接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="userId",value = "用户ID",required = true
                    ,dataType = "String",paramType = "form") ,
            @ApiImplicitParam(name="videoSeconds",value = "视频时长",required = true
                    ,dataType = "Double",paramType = "form"),
            @ApiImplicitParam(name="videoWidth",value = "视频宽度",required = true
                    ,dataType = "Integer",paramType = "form"),
            @ApiImplicitParam(name="videoHeight",value = "视频高度",required = true
                    ,dataType = "Integer",paramType = "form")
    })
    @PostMapping(value = "/user/uploadVideo",headers = "content-type=multipart/form-data")
    public IJsonResult uploadVideo(String userId,double videoSeconds,
                                  int videoWidth,int videoHeight,
                                  @ApiParam(value="视频",required = true) MultipartFile file) throws Exception{
        String fileSpace = fileBasePath+ File.separator+"videos"+File.separator;

        //Lpk26pk8YQEz41992fffbcd1a6a3794bf7732c90c60b.mp4
        String fileName = file.getOriginalFilename();
        String filePath = fileSpace+fileName;//文件保存的路径

        File faceFile = new File(filePath);
        if(!faceFile.getParentFile().exists()){//目录不存在则创建
            faceFile.getParentFile().mkdirs();
        }
        InputStream is = file.getInputStream();
        FileOutputStream fos = new FileOutputStream(filePath);

//            IOUtils.copy(is,fos);

        //下面这段代码等同于上面IOUitls那一句
        byte[] buffer = new byte[4096];
        int n =0;
        while((n=is.read(buffer))!=-1){
            fos.write(buffer,0,n);
        }

        is.close();
        fos.flush();//网络环境需要清空管道缓存，如果是本地这步可以省略
        fos.close();

        return IJsonResult.ok();
    }

}
